<template>
  <el-dialog
    v-el-drag-dialog
    :title="'自有瓶录入'"
    :visible.sync="visible"
    width="500px"
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :rules="rules" :model="temp" label-position="right">
      <el-form-item label="气瓶编码:" prop="bottleCode">
        <el-input v-model.trim="temp.bottleCode" :maxlength="11" :style="'width: 275px'" clearable />
      </el-form-item>
      <el-form-item prop="orgSNNo" label="归属机构:" class="merge-item">
        <el-tree-select
          v-model="orgSNNo"
          clearable
          class="tree-select"
          :popover-width="360"
          :select-params="{
            clearable: true,
            placeholder: '请选择',
          }"
          :tree-params=" {
            'expand-on-click-node': false,
            'highlight-current': true,
            'filterable': true,
            props:{
              value:'orgSNNo',
              label: 'orgName',
              disabled: 'disabled',
            },
            data: treeData
          }"
          @node-click="selectTreeChange"
        />
      </el-form-item>
      <el-form-item label="归属配送员:" prop="ownerId" label-width="92px">
        <el-select v-model="temp.ownerId" placeholder="请选择归属配送员" :disabled="type === 'edit'">
          <el-option
            v-for="item in distributorList"
            :key="item.value"
            :label="item.userName"
            :value="item.userId"
          />
        </el-select>
        <el-button type="primary" size="small" @click.prevent="add">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      style="width: 100%;"
    >
      <el-table-column
        prop="bottleCode"
        label="气瓶编号"
      />
      <el-table-column
        prop="orgName"
        label="归属机构"
      />
      <el-table-column
        prop="owner"
        label="归属配送员"
      />
      <el-table-column
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="delClick(scope.row,scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div slot="footer" class="dialog-footer">
      <el-button @click="reset">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.add" type="primary" @click="success">
        提交
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryOwnersList, selfBottleSave } from '@/api'

  export default {
    name: 'add',
    props: {
      treeData: {
        type: Array,
        default: () => []
      }
    },
    data() {
      return {
        visible: false,
        type: 'add',
        orgSNNo: '',
        orgInfo: undefined,
        temp: {
          bottleCode: '',
          ownerId: ''
        },
        rules: {},
        distributorList: [],
        tableData: []
      }
    },
    methods: {
      open(temp) {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.temp = Object.assign(this.temp, temp)
          }
        })
      },
      // 选择所属机构
      selectTreeChange(value) {
        this.orgInfo = value
        this.temp.ownerId = ''
        this.$set(this, 'distributorList', [])
        this.getOwnersList(value.orgId)
      },
      // 查询处理人列表
      async getOwnersList(org) {
        const res = await queryOwnersList({
          orgId: org || ''
        })
        this.distributorList = res.data
      },
      async success() {
        console.log(this.tableData)
        if (!(this.tableData.length > 0)) {
          this.$message.warning('请先添加后操作')
          return
        }
        const params = this.tableData
        await selfBottleSave(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '操作成功',
          type: 'success',
          duration: 2000
        })
      },
      async add() {
        const item = this.tableData.find(item => item.bottleCode === this.temp.bottleCode)
        if (!item) {
          if (!this.temp.bottleCode) {
            this.$message.error('请输入气瓶编码')
            return
          }
          if (!this.orgSNNo) {
            this.$message.error('请选择归属机构')
            return
          }
          if (!this.temp.ownerId) {
            this.$message.error('请选择归属配送员')
            return
          }

          this.tableData.push({
            bottleCode: this.temp.bottleCode,
            orgId: this.orgInfo.orgId,
            orgName: this.orgInfo.orgName,
            orgSNNo: this.orgSNNo,
            owner: this.temp.ownerId && this.distributorList.find(item => item.userId === this.temp.ownerId).userName,
            ownerId: this.temp.ownerId
          })
        } else {
          this.$message.error('气瓶号重复')
        }
      },
      reset() {
        this.temp = {
          bottleCode: '',
          ownerId: ''
        }
        this.orgSNNo = ''
        this.tableData = []
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
        this.visible = false
      },
      delClick(row, i) {
        this.tableData.splice(i.$index, 1)
      }
    }
  }
</script>

<style lang="scss" scoped>
.tree-select {
  width: 274px;
}
</style>
